package xcmg.device.web.warehouse;

import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import xcmg.device.dao.entity.safetyStock.MaterialsSafetyStock;
import xcmg.device.infra.AuthCode;
import xcmg.device.infra.BasicController;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.basic.MaterialsService;
import xcmg.device.service.vo.FunctionResult;
import xcmg.device.service.vo.basic.MaterialsSearchVO;
import xcmg.device.service.vo.basic.MaterialsVO;
import xcmg.device.service.vo.warehouse.SafeInventoryVO;
import xcmg.device.service.warehouse.SafeInventoryService;
import yb.ecp.fast.infra.annotation.FastMappingInfo;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/safeInventory")
public class SafeInventoryController extends BasicController {
    @Autowired
    private SafeInventoryService safeInventoryService;

    @Autowired
    private MaterialsService materialsService;

    @RequestMapping(value = "/list",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult list(@RequestBody SearchCommonVO<MaterialsSearchVO> searchVO)throws Exception{
        if(StringUtils.isEmpty(searchVO.getFilters().getOrgId())){
            searchVO.getFilters().setOrgId(getOrgId());
        }
        PageCommonVO<MaterialsVO> result=materialsService.list(searchVO);
        return actionValueResult(result.getPageInfo());
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ApiOperation(value = "更新安全库存")
    @FastMappingInfo(needLogin = true,code = AuthCode.SafeInventory.UPDATE)
    public ActionResult update(@RequestBody List<SafeInventoryVO> inventoryVOList)throws Exception{
        try {
            return actionResult(safeInventoryService.saveSafeInventory(inventoryVOList));
        }catch (Exception e){
            return new ActionResult(ErrorCode.Failure.getCode(), e.getMessage());
        }
    }

    @RequestMapping(value = "/updateAll",method = RequestMethod.POST)
    @ApiOperation(value = "更新全部的安全库存")
    @FastMappingInfo(needLogin = true,code = AuthCode.SafeInventory.UPDATE_ALL)
    public void updateAll()throws Exception{
        SafeInventoryVO condition = new SafeInventoryVO();
        condition.setOrgId(getOrgId());
        safeInventoryService.saveSafeInventoryAll(condition);
    }

    @RequestMapping(value = "/exportAll",method = RequestMethod.POST)
    @ApiOperation("导出全部安全库存记录列表")
    @FastMappingInfo(needLogin = true,code = AuthCode.SafeInventory.EXPORT)
    public void exportAll(MaterialsSafetyStock condition)throws Exception {
        condition.setOrgId(getOrgId());
        safeInventoryService.exportAll(condition);
    }

    @ResponseBody
    @RequestMapping(value = "/importSafeAmount", method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult importSafeAmount(HttpServletRequest request) throws Exception{
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        MultipartFile file = multipartRequest.getFile(multipartRequest.getFileNames().next());
        String fileName = file.getOriginalFilename();
        FunctionResult<String> result=new FunctionResult<>();
        if (!fileName.trim().endsWith(".xls")) {
            result.setCode(ErrorCode.ImportExcelEndWithXLS);
        } else {
            result= safeInventoryService.importSafeAmount(file, getOrgId(), getUserId(), getUserName());
        }
        return actionResult(result.getCode(), result.getValue());
    }

    @RequestMapping(value = "/downloadLimitExcel",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public void downloadLocationExcel() throws Exception {
        safeInventoryService.downloadLimitExcel();
    }
}
